package com.coocaa.ops.common.core.base.mybatis;

import com.coocaa.ops.common.tool.json.JsonUtil;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * @author bijiahao
 * @date : 2018/9/5.
 * @description: 类型转化-<code>List<Integer></code>
 *
 */
public class IntListTypeHandler extends BaseTypeHandler<List<Integer>> {

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, List<Integer> parameter, JdbcType jdbcType) throws SQLException {
        ps.setString(i, JsonUtil.obj2Json(parameter));
    }

    @Override
    public List<Integer> getNullableResult(ResultSet rs, String columnName) throws SQLException {
        return JsonUtil.convertToList(rs.getString(columnName), Integer[].class);
    }

    @Override
    public List<Integer> getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        return JsonUtil.convertToList(rs.getString(columnIndex), Integer[].class);
    }

    @Override
    public List<Integer> getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        return JsonUtil.convertToList(cs.getString(columnIndex), Integer[].class);
    }
}
